<!DOCTYPE html>





<html class="theme-next muse use-motion" lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="generator" content="Hexo 3.9.0">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=7.3.0">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=7.3.0">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=7.3.0">
  <link rel="mask-icon" href="/images/logo.svg?v=7.3.0" color="#222">

<link rel="stylesheet" href="/css/main.css?v=7.3.0">


<link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css?v=4.7.0">


<script id="hexo-configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Muse',
    version: '7.3.0',
    exturl: false,
    sidebar: {"position":"left","display":"post","offset":12,"onmobile":false},
    back2top: {"enable":true,"sidebar":false,"scrollpercent":false},
    save_scroll: false,
    copycode: {"enable":false,"show_result":false,"style":null},
    fancybox: false,
    mediumzoom: false,
    lazyload: false,
    pangu: false,
    algolia: {
      appID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    },
    localsearch: {"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},
    path: '',
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    translation: {
      copy_button: '复制',
      copy_success: '复制成功',
      copy_failure: '复制失败'
    }
  };
</script>

  <meta name="description" content="使用CMake编译开始使用find_package找到QT库和文件，推荐方法是用target_link_libraries 命令，这个命令回自动的添加头文件目录，编译宏，地址无关代码和链接到qtmain这个库。 例子： 12345678910111213141516171819202122cmake_minimum_required(VERSION 3.1.0)project(helloworld">
<meta name="keywords" content="CMake编译QT">
<meta property="og:type" content="article">
<meta property="og:title" content="CMake编译QT">
<meta property="og:url" content="http://yoursite.com/2019/08/01/CMake编译QT/index.html">
<meta property="og:site_name" content="XuQi&#39;s Blog">
<meta property="og:description" content="使用CMake编译开始使用find_package找到QT库和文件，推荐方法是用target_link_libraries 命令，这个命令回自动的添加头文件目录，编译宏，地址无关代码和链接到qtmain这个库。 例子： 12345678910111213141516171819202122cmake_minimum_required(VERSION 3.1.0)project(helloworld">
<meta property="og:locale" content="zh-CN">
<meta property="og:updated_time" content="2019-10-20T13:23:23.037Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="CMake编译QT">
<meta name="twitter:description" content="使用CMake编译开始使用find_package找到QT库和文件，推荐方法是用target_link_libraries 命令，这个命令回自动的添加头文件目录，编译宏，地址无关代码和链接到qtmain这个库。 例子： 12345678910111213141516171819202122cmake_minimum_required(VERSION 3.1.0)project(helloworld">
  <link rel="canonical" href="http://yoursite.com/2019/08/01/CMake编译QT/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome: false,
    isPost: true,
    isPage: false,
    isArchive: false
  };
</script>

  <title>CMake编译QT | XuQi's Blog</title>
  








  <noscript>
  <style>
  .use-motion .motion-element,
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-title { opacity: initial; }

  .use-motion .logo,
  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-CN">

  <div class="container sidebar-position-left">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta">

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">XuQi's Blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
  </div>

  <div class="site-nav-toggle">
    <button aria-label="切换导航栏">
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>


<nav class="site-nav">
  
  <ul id="menu" class="menu">
      
      
      
        
        <li class="menu-item menu-item-home">
      
    

    <a href="/" rel="section"><i class="menu-item-icon fa fa-fw fa-home"></i> <br>首页</a>

  </li>
      
      
      
        
        <li class="menu-item menu-item-archives">
      
    

    <a href="/archives/" rel="section"><i class="menu-item-icon fa fa-fw fa-archive"></i> <br>归档</a>

  </li>
  </ul>

    

</nav>
</div>
    </header>

    


    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content page-post-detail">
            

  <div id="posts" class="posts-expand">
    

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2019/08/01/CMake编译QT/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="XuQi">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="XuQi's Blog">
    </span>
      <header class="post-header">

        
          <h1 class="post-title" itemprop="name headline">CMake编译QT

            
          </h1>
        

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              
                
              

              <time title="创建时间：2019-08-01 20:28:31" itemprop="dateCreated datePublished" datetime="2019-08-01T20:28:31+00:00">2019-08-01</time>
            </span>
          
            

            
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2019-10-20 13:23:23" itemprop="dateModified" datetime="2019-10-20T13:23:23+00:00">2019-10-20</time>
              </span>
            
          

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h3 id="使用CMake编译"><a href="#使用CMake编译" class="headerlink" title="使用CMake编译"></a>使用CMake编译</h3><h4 id="开始"><a href="#开始" class="headerlink" title="开始"></a>开始</h4><p>使用<code>find_package</code>找到QT库和文件，推荐方法是用<code>target_link_libraries</code> 命令，这个命令回自动的添加头文件目录，编译宏，地址无关代码和链接到qtmain这个库。</p>
<p>例子：</p>
<figure class="highlight cmake"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">cmake_minimum_required</span>(VERSION <span class="number">3.1</span>.<span class="number">0</span>)</span><br><span class="line"></span><br><span class="line"><span class="keyword">project</span>(helloworld)</span><br><span class="line"></span><br><span class="line"><span class="keyword">set</span>(CMAKE_AUTOMOC <span class="keyword">ON</span>)</span><br><span class="line"><span class="keyword">set</span>(CMAKE_AUTORCC <span class="keyword">ON</span>)</span><br><span class="line"><span class="keyword">set</span>(CMAKE_AUTOUIC <span class="keyword">ON</span>)</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span>(CMAKE_VERSION <span class="keyword">VERSION_LESS</span> <span class="string">"3.7.0"</span>)</span><br><span class="line">    <span class="keyword">set</span>(CMAKE_INCLUDE_CURRENT_DIR <span class="keyword">ON</span>)</span><br><span class="line"><span class="keyword">endif</span>()</span><br><span class="line"></span><br><span class="line"><span class="keyword">find_package</span>(Qt5 COMPONENTS Widgets REQUIRED)</span><br><span class="line"></span><br><span class="line"><span class="keyword">add_executable</span>(helloworld</span><br><span class="line">    mainwindow.ui</span><br><span class="line">    mainwindow.cpp</span><br><span class="line">    main.cpp</span><br><span class="line">    resources.qrc</span><br><span class="line">)</span><br><span class="line"></span><br><span class="line"><span class="keyword">target_link_libraries</span>(helloworld Qt5::Widgets)</span><br></pre></td></tr></table></figure>

<p>为了<code>find_package</code>成功找到QT5，它必须在 CMAKE_PREFIX_PATH路径下，或者Qt5_DIR下必须有Qt5config.cmake文件。最简单的办法就是设置环境变量CMAKE_PREFIX_PATH为QT的按照目录。</p>
<p>同时CMAKE_AUTOMOC必须设置为ON。</p>
<p>详见<a href="https://cmake.org/cmake/help/latest/manual/cmake-qt.7.html#automoc#" target="_blank" rel="noopener">https://cmake.org/cmake/help/latest/manual/cmake-qt.7.html#automoc#</a>)</p>
<h4 id="导入模块"><a href="#导入模块" class="headerlink" title="导入模块"></a>导入模块</h4><p>引入模块可以像这样 Qt5<module>_LIBRARIES,比如在<code>target_link_libraries</code>. 实际的路径可以通过以下方式获取:</module></p>
<figure class="highlight cmake"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">find_package</span>(Qt5 COMPONENTS Core REQUIRED)</span><br><span class="line"></span><br><span class="line"><span class="keyword">get_target_property</span>(QtCore_location Qt5::Core LOCATION)</span><br></pre></td></tr></table></figure>

<p>很少需要知道完整的路径，每一个QT5模块的库，可以用Qt5::<module>方便的使用。</module></p>
<p>如果你的有自己的CMake文件，你必须设置debug或者release。例如</p>
<figure class="highlight cmake"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">find_package</span>(Qt5 COMPONENTS Core REQUIRED)</span><br><span class="line"></span><br><span class="line"><span class="keyword">set</span>(CMAKE_CXX_FLAGS_COVERAGE <span class="string">"$&#123;CMAKE_CXX_FLAGS_RELEASE&#125; -fprofile-arcs -ftest-coverage"</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># set up a mapping so that the Release configuration for the Qt imported target is</span></span><br><span class="line"><span class="comment"># used in the COVERAGE CMake configuration.</span></span><br><span class="line"><span class="keyword">set_target_properties</span>(Qt5::Core PROPERTIES MAP_IMPORTED_CONFIG_COVERAGE <span class="string">"RELEASE"</span>)</span><br></pre></td></tr></table></figure>

<h4 id="导入插件"><a href="#导入插件" class="headerlink" title="导入插件"></a>导入插件</h4><p>插件同样支持导入， <a href="https://doc.qt.io/qt-5/qtnetwork-index.html" target="_blank" rel="noopener">Qt Network</a>, <a href="https://doc.qt.io/qt-5/qtsql-index.html" target="_blank" rel="noopener">Qt SQL</a>, <a href="https://doc.qt.io/qt-5/qtgui-index.html" target="_blank" rel="noopener">Qt GUI</a>, 和 <a href="https://doc.qt.io/qt-5/qtwidgets-index.html" target="_blank" rel="noopener">Qt Widgets</a> 模块都有关联的插件，他们都提供了插件列表，在Qt5<code>&lt;Module&gt;_PLUGINS</code>变量中。</p>
<figure class="highlight cmake"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">foreach</span>(plugin <span class="variable">$&#123;Qt5Network_PLUGINS&#125;</span>)</span><br><span class="line">  <span class="keyword">get_target_property</span>(_loc <span class="variable">$&#123;plugin&#125;</span> LOCATION)</span><br><span class="line">  <span class="keyword">message</span>(<span class="string">"Plugin $&#123;plugin&#125; is at location $&#123;_loc&#125;"</span>)</span><br><span class="line"><span class="keyword">endforeach</span>()</span><br></pre></td></tr></table></figure>

<h3 id="Qt-Core"><a href="#Qt-Core" class="headerlink" title="Qt Core"></a>Qt Core</h3><p><a href="https://doc.qt.io/qt-5/qtcore-index.html" target="_blank" rel="noopener">https://doc.qt.io/qt-5/qtcore-index.html</a></p>
<p><strong>核心功能</strong></p>
<p>Qt将这些特性添加到C++中:</p>
<blockquote>
<p>1.一种非常强大的机制，用于无缝对象通信，称为信号和插槽<br>2.可查询和可设计的对象属性<br>3.组织的层次化和可查询的对象树<br>4.用保护指针(QPointer)的自然方式拥有对象所有权<br>5.跨库边界的动态转换</p>
</blockquote>
<p><strong>线程和并发编程</strong></p>
<blockquote>
<p>Qt以独立于平台的线程类的形式提供线程支持，线程安全的发布事件的方式，以及跨线程的信号槽连接。<br>多线程编程也是一种有用的范例，它可以在不冻结应用程序的用户界面的情况下执行耗时的操作。Qt页面中的线程支持包含在应用程序中实现线程的信息。额外的并发类由Qt并发模块提供。</p>
</blockquote>
<p><strong>输入/输出、资源和容器</strong></p>
<p>Qt提供了一种资源系统，用于组织应用程序文件和资产、一组容器和用于接收输入和打印输出的类:</p>
<blockquote>
<ol>
<li>容器类</li>
<li>序列化Qt数据类型</li>
<li>隐式共享</li>
<li>此外，Qt核心提供了一种独立于平台的机制，用于在应用程序的可执行文件中存储二进制文件。<br>Qt的资源系统</li>
</ol>
</blockquote>
<p><strong>额外的框架</strong></p>
<p>Qt Core还提供了Qt的一些关键框架:</p>
<blockquote>
<ol>
<li>动画框架</li>
<li>在Qt JSON支持</li>
<li>状态机框架</li>
<li>如何创建Qt插件</li>
<li>事件系统</li>
</ol>
</blockquote>
<h4 id="代码下载"><a href="#代码下载" class="headerlink" title="代码下载"></a>代码下载</h4><p><a href="http://download.qt.io/archive/qt/5.13/5.13.0/submodules/" target="_blank" rel="noopener">http://download.qt.io/archive/qt/5.13/5.13.0/submodules/</a></p>
<p><strong>QT和JS交互调用</strong></p>
<p><a href="https://blog.csdn.net/caoshangpa/article/details/51014690" target="_blank" rel="noopener">https://blog.csdn.net/caoshangpa/article/details/51014690</a></p>

    </div>

    
    
    
        
      

      <footer class="post-footer">
          
            
          
          <div class="post-tags">
            
              <a href="/tags/CMake编译QT/" rel="tag"># CMake编译QT</a>
            
          </div>
        

        

          <div class="post-nav">
            <div class="post-nav-next post-nav-item">
              
                <a href="/2019/07/17/V2X之MATLAB/" rel="next" title="V2X之MATLAB">
                  <i class="fa fa-chevron-left"></i> V2X之MATLAB
                </a>
              
            </div>

            <span class="post-nav-divider"></span>

            <div class="post-nav-prev post-nav-item">
              
                <a href="/2019/08/03/树莓派交叉编译/" rel="prev" title="树莓派交叉编译">
                  树莓派交叉编译 <i class="fa fa-chevron-right"></i>
                </a>
              
            </div>
          </div>
        
      </footer>
    
  </div>
  
  
  
  </article>

  </div>


          </div>
          

        </div>
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-overview">

          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">XuQi</p>
  <div class="site-description motion-element" itemprop="description"></div>
</div>
  <nav class="site-state motion-element">
      <div class="site-state-item site-state-posts">
        
          <a href="/archives/">
        
          <span class="site-state-item-count">44</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
    
      
      
      <div class="site-state-item site-state-tags">
        
        
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
        <span class="site-state-item-count">30</span>
        <span class="site-state-item-name">标签</span>
        
      </div>
    
  </nav>



        </div>
      </div>
      <!--noindex-->
        <div class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
            
            
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#使用CMake编译"><span class="nav-number">1.</span> <span class="nav-text">使用CMake编译</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#开始"><span class="nav-number">1.1.</span> <span class="nav-text">开始</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#导入模块"><span class="nav-number">1.2.</span> <span class="nav-text">导入模块</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#导入插件"><span class="nav-number">1.3.</span> <span class="nav-text">导入插件</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Qt-Core"><span class="nav-number">2.</span> <span class="nav-text">Qt Core</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#代码下载"><span class="nav-number">2.1.</span> <span class="nav-text">代码下载</span></a></li></ol></li></ol></div>
            

          </div>
        </div>
      <!--/noindex-->
      

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2019</span>
  <span class="with-love" id="animate">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">XuQi</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io" class="theme-link" rel="noopener" target="_blank">Hexo</a> 强力驱动 v3.9.0</div>
  <span class="post-meta-divider">|</span>
  <div class="theme-info">主题 – <a href="https://theme-next.org" class="theme-link" rel="noopener" target="_blank">NexT.Muse</a> v7.3.0</div>

        








        
      </div>
    </footer>
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
      </div>

    

  </div>

  
  <script src="/lib/jquery/index.js?v=3.4.1"></script>
  <script src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  <script src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

<script src="/js/utils.js?v=7.3.0"></script>
  <script src="/js/motion.js?v=7.3.0"></script>


  <script src="/js/schemes/muse.js?v=7.3.0"></script>


<script src="/js/next-boot.js?v=7.3.0"></script>




  




























  

  

  


  
  <script src="/js/scrollspy.js?v=7.3.0"></script>
<script src="/js/post-details.js?v=7.3.0"></script>


</body>
</html>
